package org.atf.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.Header;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.atf.dp.client.DPHttpsClient;
import org.atf.model.dao.DpServiceDAO;
import org.atf.model.entity.DpService;


public class DpController {
	DpServiceDAO dpServiceDAO= new DpServiceDAO();
	private  static Logger  logger = Logger.getLogger(DpController.class);
	public List<String> getDpServiceUniqueRegions(){	
		return dpServiceDAO.getDpServiceUniqueRegions();
	}
	
	public List<DpService> getDpServiceList(){	
		return dpServiceDAO.getDpServices();
	}
	public String addDpService(DpService dpService){
		//logger.debug("adding new Service:"+dpService.toString());
		dpServiceDAO.insertDpService(dpService);
		String result="OK";
		return result;
	}
	
	public String updateDpService(DpService dpService){
	//	logger.debug("Updating new Service:"+dpService.toString());
		dpServiceDAO.updateDpService(dpService);
		String result="OK";
		return result;
	}
	public String deleteDpService(Integer id){
		dpServiceDAO.deleteDpService(id);
		return "OK";
	}
	
	public List<DpService> getDpGetAllServiceByRegion(String regionName){
		return dpServiceDAO.getDpGetAllServiceByRegion(regionName);
	}
	
	public DpService getEmptyDpService(){
		return new DpService();
	}
	
	public HashMap<String,String> executeDPService(Integer id){
		DpService dpService=dpServiceDAO.getDpServiceById(id);
		HashMap<String,String> result=new HashMap<String,String>();
		if(dpService != null){
			if(dpService.getUrl() == null || dpService.getUrl().equalsIgnoreCase("")){
				result.put("ERROR", "Empty End Point URL");
				
			}
			HashMap<String,String> headerMap= new HashMap<String,String>();
			if(dpService.getHttpHeaders() != null){
				if(dpService.getHttpHeaders().equalsIgnoreCase("")){
					String[] tempArray=dpService.getHttpHeaders().split(",");
					for(String headerNameValuePair:tempArray){
						String[] headerNameValuePairArray=headerNameValuePair.split("=");
						if( headerNameValuePairArray.length >= 2){
							String name=headerNameValuePairArray[0];
							String value=headerNameValuePairArray[1];
							headerMap.put(name, value);
						}
					}
				}
			}
			DPHttpsClient dpHttsClient = new DPHttpsClient();
			
			try {
				dpHttsClient.setUrl(dpService.getUrl());
				if(!headerMap.isEmpty()){
					dpHttsClient.setHeaders(headerMap);
				}
				if (dpService.getSoapAction() != null ){
					dpHttsClient.setSoapAction(dpService.getSoapAction());
				}
				if (dpService.getContentType() != null ){
					dpHttsClient.setContentType(dpService.getContentType());
				}
				dpHttsClient.setUsername(dpService.getUsername());
				dpHttsClient.setPassword(dpService.getPassword());
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				result.put("ERROR", e1.getMessage());
			}		
			
			
			if(dpService.getType().equalsIgnoreCase("SOAP")){
				try {
					dpHttsClient.connect("POST");
					String response=dpHttsClient.sendMessage(dpService.getPayload());
					result.put("RESPONSE", response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result.put("ERROR", e.getMessage());
				}
				
			}else if(dpService.getType().equalsIgnoreCase("HTTP")){
				try {
					dpHttsClient.connect(dpService.getHttpMethod());
					String response=dpHttsClient.sendMessage(dpService.getPayload());
					result.put("RESPONSE", response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result.put("ERROR", e.getMessage());
				}
				
			}else{
				result.put("ERROR", "Uknow Type");
				
			}
		}else{
			result.put("ERROR", "Service Not Found");
		}
		
		return result;
	}
}
